/**
 * Created by lihao on 2017/3/20.
 */
var Html = require('../../../component/common/workNav.html');
require('../../../css/component/common/workNav.less');
UserService = require('../../module/UserService');
var config = require('../../etc/config');

avalon.component('ms-work-nav', {
    template: Html,
    defaults: {
        isActive: '',
        list: config.workNav,
        dom: '',
        isShow: false,
        onInit: function (e) {
            var _this = this;
            this.dom = $(e.target);
            var locationUrl = window.location.pathname;
            $('body').on('userIsLoad', function () {
                var permissions = UserService.getUserData().permissions;
                _this.list = _.compact(_.map(_this.list, function (nav) {
                    if (nav.sub.length) {
                        nav.sub = _.compact(_.map(nav.sub, function (sub) {
                            // if (sub.authority.length) {
                            //     var hasAuth = _.find(permissions, function (n) {
                            //         return n.permissionOperate == sub.authority;
                            //     });
                            //     if (hasAuth == undefined) sub = ''
                            // }
                            return sub;
                        }));
                    }
                    if(nav.url == 'javascript:void(0);' && nav.sub.length == 0){
                        nav = '';
                    }else if(nav.authority.length){
                        // var hasAuth = _.find(permissions, function (n) {
                        //     return n.permissionOperate == nav.authority;
                        // });
                        // if (!hasAuth) nav = ''
                    }
                    return nav;
                }));
                _this.isShow = true;
            });
            this.list = _.map(this.list, function (nav) {
                if (locationUrl == nav.url) {
                    nav.isActive = true;
                }
                if (nav.sub.length) {
                    _.map(nav.sub, function (sub) {
                        if (locationUrl == sub.url) {
                            sub.isActive = true;
                        }
                        if (sub.activeUrl.length && sub.isActive == false) {
                            sub.isActive = _.indexOf(sub.activeUrl, locationUrl) >= 0;
                        }
                        return sub;
                    });
                    var hasActive = _.countBy(nav.sub, 'isActive');
                    nav.isActive = hasActive.true ? true : false;
                }
                if (nav.activeUrl.length && nav.isActive == false) {
                    nav.isActive = _.indexOf(nav.activeUrl, locationUrl) >= 0;
                }
                return nav;
            });
        },
        onReady: function () {
            this.dom.find('.hasSub').each(function () {
                $(this).on('click', function () {
                    $(this).next('.sub').toggle();
                })
            });
        }
    }
});
